/*
Copyright 2010,2011 Matt Van Der Westhuizen

This file is part of CGLL.

CGLL is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

CGLL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with CGLL.  If not, see <http://www.gnu.org/licenses/>.
*/

package org.chaoticengine.cgll.factory;

import java.io.IOException;
import java.io.InputStream;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.particles.ParticleIO;
import org.newdawn.slick.particles.ParticleSystem;

/**
 * The particle factory loads each particle effect once and then issues
 * duplicates of these resources for use by other classes, to avoid incurring
 * the high cost of parsing XML every time a particle effect is needed.
 *
 * @author Matt v.d. Westhuizen
 */
public class ParticleEffectFactory extends AbstractFactory {
    private static ParticleEffectFactory instance = null;

    private ParticleEffectFactory() {}

    public static ParticleEffectFactory getInstance() {
        if (instance == null) {
            instance = new ParticleEffectFactory();
        }

        return(instance);
    }

    public ParticleSystem getEffect(String filename) throws SlickException {
        InputStream is = getCachedResource("data/particles/" + filename);

        ParticleSystem ps = null;
        try {
            ps = ParticleIO.loadConfiguredSystem(is);
        } catch (IOException ioEx) {
            throw new SlickException("Failed to load particle system '" +
                    filename + "' with reason '" + ioEx.getMessage() + "'.");
        }

        return(ps);
    }
}
